//
//  TechSelectView.m
//  TechGoldology
//
//  Created by 陈名邦 on 2018/10/25.
//  Copyright © 2018年 陈名邦. All rights reserved.
//

#import "TechSelectView.h"

@implementation TechSelectView

- (instancetype)init{
    
    self = [super init];
    if (self) {
        [self initViews];
    }
    return self;
    
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initViews];
    }
    return self;
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self initViews];
    }
    return self;
}

- (void)initViews{
    
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.7];
    _coverView = [[UIView alloc] initWithFrame:CGRectMake(8, 200.f, self.frame.size.width - 16, self.frame.size.height-300)];
    _coverView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_coverView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(4, 8, _coverView.frame.size.width - 8, _coverView.frame.size.height - 48)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
    [_coverView addSubview:_tableView];
    
    _cancelbutton = [[UIButton alloc] initWithFrame:CGRectMake(_coverView.t_right - 96.f, _tableView.t_bottom + 8, 96.f, 24.f)];
    [_cancelbutton setTitle:@"取消"
                   forState:UIControlStateNormal];
    [_cancelbutton setTitleColor:[UIColor blueColor]
                        forState:UIControlStateNormal];
    [_cancelbutton addTarget:self
                      action:@selector(cancelAction)
            forControlEvents:UIControlEventTouchUpInside];
    [_coverView addSubview:_cancelbutton];
    
}

- (void)cancelAction{
    
    [self removeFromSuperview];
    
}

#pragma mark - UITableviewdelegate,UITableViewdatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    NSDictionary *dic = [_dataArr objectAtIndex:indexPath.row];
    //这里根据viewtype类型，选择输入的文本
    if ([_viewType isEqualToString:@"Viewtype"]) {
        
        cell.textLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@""]];
        
    }else{
        
        cell.textLabel.text = [dic objectForKey:@"title"];
        
    }
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 48.f;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44.f;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return _viewTitle;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath
                             animated:NO];
    [_delegate selectItem:[_dataArr objectAtIndex:indexPath.row] withType:_viewType];
    [self removeFromSuperview];
    
}
@end
